package com.wetao.lib_common.utils;

import android.os.Environment;
import android.util.Log;

import com.wetao.lib_common.Constants;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 打印log日志工具类
 */
public class LogUtils {
    private static boolean LOG = true;
    private static boolean IS_SAVE_FILE = false;
    private static boolean LOGV = true;
    private static boolean LOGD = true;
    private static boolean LOGI = true;
    private static boolean LOGW = true;
    private static boolean LOGE = true;

    //private static String FILE_PATH = Environment.getExternalStorageDirectory() + "/学捕头/log.txt";
    private static String FILE_PATH = Constants.FILE_PATH + "/学捕头/log.txt";
    private static int FILE_MAX_LENGTH = 10485760;

    public static void setLogEnable(boolean enable) {
        LOG = enable;
    }
    public static void setLogEnablePrint(boolean enable) {
        IS_SAVE_FILE = enable;
    }
    public static void setLogPrintFile(String filePath){
        FILE_PATH = filePath;
    }
    public static void setLogPrintFileMax(int fileMaxLength){
        FILE_MAX_LENGTH = fileMaxLength;
    }

    public static void v(String mess) {
        if (LOG && LOGV) {
            Log.v(getTag(), buildMessage(mess));
        }
        if(IS_SAVE_FILE){
            writeLogAppend(getTag(), mess);
        }
    }

    public static void d(String mess) {
        if (LOG && LOGD) {
            Log.d(getTag(), buildMessage(mess));
        }
        if(IS_SAVE_FILE){
            writeLogAppend(getTag(), mess);
        }
    }

    public static void i(String mess) {
        if (LOG && LOGI) {
            Log.i(getTag(), buildMessage(mess));
        }
        if(IS_SAVE_FILE){
            writeLogAppend(getTag(), mess);
        }
    }

    public static void w(String mess) {
        if (LOG && LOGW) {
            Log.w(getTag(), buildMessage(mess));
        }
        if(IS_SAVE_FILE){
            writeLogAppend(getTag(), mess);
        }
    }

    public static void e(String mess) {
        if (LOG && LOGE) {
            log(getTag(), buildMessage(mess));
        }
        if(IS_SAVE_FILE){
            writeLogAppend(getTag(), mess);
        }
    }

    public static void log(String tag, String msg){
        if(msg.length() > 4000) {
            for(int i=0;i<msg.length();i+=4000){
                if(i+4000<msg.length())
                    Log.e(tag,msg.substring(i, i+4000));
                else
                    Log.e(tag,msg.substring(i, msg.length()));
            }
        } else
            Log.e(tag,msg);
    }

    public static String getTag() {
        StackTraceElement[] trace = new Throwable().fillInStackTrace()
                .getStackTrace();
        String callingClass = "";
        for (int i = 2; i < trace.length; i++) {
            Class<?> clazz = trace[i].getClass();
            if (!clazz.equals(LogUtils.class)) {
                callingClass = trace[i].getClassName();
                callingClass = callingClass.substring(callingClass
                        .lastIndexOf('.') + 1);
                break;
            }
        }
        return callingClass;
    }

    private static String buildMessage(String msg) {
        StackTraceElement[] trace = new Throwable().fillInStackTrace()
                .getStackTrace();
        String caller = "";
        for (int i = 2; i < trace.length; i++) {
            Class<?> clazz = trace[i].getClass();
            if (!clazz.equals(LogUtils.class)) {
                caller = trace[i].getMethodName();
                break;
            }
        }
        return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread()
                .getId(), caller, msg);
    }

    public static void writeLogAppend(String tag, String msg) {
        File file = new File(FILE_PATH);
        if (!file.exists()) {
            String directory = FILE_PATH.substring(0, FILE_PATH.lastIndexOf("/"));
            File dirFile = new File(directory);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }

            try {
                file.createNewFile();
            } catch (IOException var8) {
                var8.printStackTrace();
            }
        }

        try {
            RandomAccessFile randomFile = new RandomAccessFile(FILE_PATH, "rw");
            long fileLength = randomFile.length();
            if (fileLength > (long)FILE_MAX_LENGTH) {
                randomFile.setLength(0L);
                fileLength = 0L;
            }

            randomFile.seek(fileLength);
            randomFile.write(String.format("\n%s %s %s", getCurTime(), tag, msg).getBytes("gb2312"));
            randomFile.close();
        } catch (FileNotFoundException var6) {
            var6.printStackTrace();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

    }

    private static String getCurTime() {
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss.sss");
        Date date = new Date(System.currentTimeMillis());
        return format.format(date);
    }

}
